package cate.game.role.res.guild.po.my;

import cate.common.table.d.GDFunc;
import cate.common.util.GameResult;
import cate.game.role.Role;
import cate.common.table.guild.GuildEnvelopeRow;
import cate.game.role.res.guild.boss.GuildFuncImpl;
import cate.game.role.res.guild.msg.envelope.MyGuildEnvelopeResp;
import cate.game.role.res.guild.po.GuildPO;

import cate.game.role.res.guild.po.GuildFuncDependent;
import cate.game.role.task.conditiontask.ConditionFuncPO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;

import java.util.*;

public class MyGuildRedEnvelopePO extends ConditionFuncPO<GuildEnvelopeRow> implements GuildFuncImpl {
    @NoteField(value = "今日抢红包次数")
    public int drawTime;

    @NoteField(value = "我的福利红包", detail = "map<福利红包ID，福利红包个数")
    public Map<Integer, Integer> benefitMap;

    @NoteField(value = "我的福利红包", detail = "map<红包ID，发放过的个数")
    public Map<Integer, Integer> sendMap;

    @JsonIgnore
    @NoteField(value = "今日发红包次数")
    public int distributeTime;

    public void addTime() {
        drawTime++;
        noticeUpdate();
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (benefitMap == null) {
            benefitMap = new HashMap<>();
        }
        if (sendMap == null) {
            sendMap = new HashMap<>();
        }
    }

    @Override
    protected GuildEnvelopeRow getRow(int tid) {
        return role.getGame().table.guild.envelope.get(tid);
    }

    @Override
    protected List<Integer> checkSelfCondition() {
        List<Integer> tId = new ArrayList<>();
        List<GuildEnvelopeRow> envelopRows = role.getGame().table.guild.envelope.getList();
        if (envelopRows == null) {
            return Collections.emptyList();
        }
        envelopRows.forEach(row -> tId.add(row.tid));
        return tId;
    }

    @Override
    protected void doDone(int tid) {
        GuildEnvelopeRow row = getRow(tid);
        if (row == null) {
            return;
        }
        if (benefitMap.containsKey(tid)) {
            int num = benefitMap.get(tid) + 1;
            benefitMap.put(tid, num);
        } else {
            benefitMap.put(tid, 1);
        }
        noticeUpdate();
    }

    @Override
    protected void selfClear() {
    }

    /**
     * 跨天更新
     */
    @Override
    public void onDaySpan() {
        super.onDaySpan();
        if (drawTime != 0) {
            drawTime = 0;
            noticeUpdate();
        }
    }

    private void noticeUpdate() {
        role.sendNow(new MyGuildEnvelopeResp(role));
    }

    /**
     * 发放福利红包
     */
    public void sendRedEnveLope(int id) {
        if(!guildFuncOpenCheck(role)){
            return;
        }
        GuildPO guild = role.getRes().guild.getGuild();

        GameResult<Void> r = sendRedEnveLopeCheck(id);
        if (!r.ok()) {
            role.getGame().notice.message(role, r.message);
            return;
        }

        guild.envelope.afterPay(role, id);
    }

    private GameResult<Void> sendRedEnveLopeCheck(int id) {
        GameResult<Void> r = new GameResult<>();
        GuildEnvelopeRow row = role.getGame().table.guild.envelope.get(id);
        if (row == null) {
            return r.fail("所选红包不存在");
        }
        Integer num = benefitMap.get(id);
        if (num == null) {
            return r.fail("没有获取该红包");
        }

        if (num == 0) {
            return r.fail("没有获取该红包");
        }
        num--;
        benefitMap.put(id, num);
        return r.success();
    }

    @Override
    public GuildFuncDependent getFuncDependent() {
        if (role.getRes().guild.checkGuild().ok()) {
            return createFuncDependent(role.getRes().guild.getGuild(), role);
        }
        return createFuncDependent(null, role);
    }

    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.ENVELOP;
    }
}
